package Controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import Entity.Worker;
import 	Communication.Client;
import Controller.WorkerMainMenuController;
import View.WorkerMainMenuView;

import javax.swing.JButton;

import Controller.PatternController;


/** This class controls and manages the process that let the company worker the abilities to make actions in the system */
public class WorkerMainMenuController {
	/**  Required attributes */
	private WorkerMainMenuView workerMainMenuView;
	private Client client;
	private Worker worker ;

	/**
	 * Constructor calling to listener class that attached to the process
	 * @param client
	 * @param loginController
	 */
	public WorkerMainMenuController(Client client,
			LoginController loginController) {
		this.client = client;
		workerMainMenuView = new WorkerMainMenuView();		
		workerMainMenuView.addWorkerMainMenuListener(new WorkerMainMenuListener(loginController));
	}


	/**  Inner Listener class */ 
	class WorkerMainMenuListener implements ActionListener {
		LoginController 				loginController;
		AddNewCustomerController  		addNewCustomerController;
		RunSaleController      			runSaleController;
		PatternController   			patternController;
		UpdateCustomerController  		updateCustomerController;
		NewProductToCatalogController 	newProductToCatalogController;
		ReportsController        		reportsController;
		HandleNewSaleController 		handleNewSaleController;
		HandleCityController 			handleCityController;
		SettingDistributionsController 	settingDistController;


		/**
		 * creats WorkerMainMenu Listener
		 * @param loginController
		 */
		public WorkerMainMenuListener(LoginController loginController) {
			this.loginController = loginController;	
		}

		/**  Handle Actions*/
		public void actionPerformed(ActionEvent event) {
			if (event.getSource() == workerMainMenuView.getEnterNewCustomerBtn()) {
				addNewCustomerController = new AddNewCustomerController(
						client, WorkerMainMenuController.this);
			}
			else if (event.getSource() == workerMainMenuView.getProductToCatalogBtn()) {
				newProductToCatalogController = new NewProductToCatalogController(
						client, WorkerMainMenuController.this);
			}
			else if (event.getSource() == workerMainMenuView.getUpdateCustomerBtn()) {
				updateCustomerController = new UpdateCustomerController(
						client, WorkerMainMenuController.this);
			}
			else if (event.getSource() == workerMainMenuView. getNewPatternBtn()) {
				patternController= new PatternController(
						client, WorkerMainMenuController.this);
			}
			else if (event.getSource() == workerMainMenuView.getProductToCatalogBtn()) {
				newProductToCatalogController= new NewProductToCatalogController(
						client, WorkerMainMenuController.this); 
			}
			else if (event.getSource() == workerMainMenuView.getRunSaleBtn()) {
				runSaleController= new RunSaleController(
						client, WorkerMainMenuController.this);
			}
			else if (event.getSource() == workerMainMenuView.getReportsnBtn()) {
				reportsController= new ReportsController(
						client, WorkerMainMenuController.this);
			}
			else if(event.getSource() == workerMainMenuView.getHandleSaleBtn()){
				handleNewSaleController= new HandleNewSaleController(
						client, WorkerMainMenuController.this);
			}
			else if (event.getSource() == workerMainMenuView. getHandleCatalogBtn()) {
				settingDistController= new SettingDistributionsController(
						client, WorkerMainMenuController.this);
			}
			else if(event.getSource() == workerMainMenuView.getHandleCityBtn()){
				handleCityController= new HandleCityController(
						client, WorkerMainMenuController.this);
			}
			else if (event.getSource() == workerMainMenuView. getLogoutBtn()) {
				worker=new Worker ();
				new LoginController(client);
				worker.setStatusDB(client,  0);
				workerMainMenuView.hideWindow();
			}
		}
	}

	/**
	 * setting the available buttons according to worker role
	 * @param availableButtons
	 */
	public void setActiveButtons(boolean[] availableButtons) {

		if (availableButtons[0] == false)
			workerMainMenuView.getEnterNewCustomerBtn().setEnabled(false);

		if (availableButtons[1] == false)
			workerMainMenuView.getNewPatternBtn().setEnabled(false);

		if (availableButtons[2] == false)
			workerMainMenuView.getProductToCatalogBtn().setEnabled(false);

		if (availableButtons[3] == false)
			workerMainMenuView.getRunSaleBtn().setEnabled(false);

		if (availableButtons[4] == false)
			workerMainMenuView.getHandleSaleBtn().setEnabled(false);

		if (availableButtons[5] == false)
			workerMainMenuView.getReportsnBtn().setEnabled(false);

		if (availableButtons[6] == false)
			workerMainMenuView.getHandleCatalogBtn().setEnabled(false);

		if (availableButtons[7] == false)			
			workerMainMenuView.getUpdateCustomerBtn().setEnabled(false);

		if (availableButtons[8] == false)			
			workerMainMenuView.getHandleCityBtn().setEnabled(false);

		workerMainMenuView.getLogoutBtn().setEnabled(true);
	}


	/** setting all buttons to available*/
	public void enableAll() {
		workerMainMenuView.getEnterNewCustomerBtn().setEnabled(true);

		workerMainMenuView.getNewPatternBtn().setEnabled(true);

		workerMainMenuView.getProductToCatalogBtn().setEnabled(true);

		workerMainMenuView.getRunSaleBtn().setEnabled(true);

		workerMainMenuView.getHandleSaleBtn().setEnabled(true);

		workerMainMenuView.getReportsnBtn().setEnabled(true);

		workerMainMenuView.getHandleCatalogBtn().setEnabled(true);

		workerMainMenuView.getUpdateCustomerBtn().setEnabled(true);

		workerMainMenuView.getHandleCityBtn().setEnabled(true);

		workerMainMenuView.getLogoutBtn().setEnabled(true);

	}


	/**
	 *  shows worker main menu window
	 */
	public void showWindow() {
		workerMainMenuView.showWindow();
	}



	/**
	 *  Hides worker main menu window
	 */
	public void hideWindow() {
		workerMainMenuView.hideWindow();
	}


}





